package com.abel.mvc.listener;

import com.abel.mvc.entity.Mail;
import com.abel.mvc.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

/**
 * @author abel.he
 * @data 2018/8/11 11:36
 */
@Component("myListener")
public class Mylistener implements MessageListener {

    @Autowired
    MessageService messageService;

    @Override
    public void onMessage(Message message) {
        try {
            if (message instanceof ObjectMessage) {
                ObjectMessage objectMessage = (ObjectMessage) message;
                Object obj = objectMessage.getObject();
                if (obj instanceof Mail) {
                    final Mail mail = (Mail) obj;
                    messageService.sendMail(mail);
                }
            } else {
                System.out.println( message);
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
